package com.code.splider.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xml.sax.SAXException;

import com.code.splider.service.ScorePanluService;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

@Controller
@RequestMapping("scorePanlu")
public class ScorePanluController {
	
	@Autowired
	private ScorePanluService  scorePanluService;
	
	@RequestMapping(value = "save")
	public String save() throws IOException, SAXException {
		System.out.println("向服务器发送数据，然后获取网页内容：");
        WebConversation wc = new WebConversation();
        HttpUnitOptions.setExceptionsThrownOnScriptError(false);// js运行错误时，是否抛出异常

        WebRequest req = new GetMethodWebRequest("http://live.dszuqiu.com/ajax/score/data");
        req.setParameter("mt", "0");
        req.setParameter("nr", "1");
       
        //获取响应对象
        WebResponse resp = wc.getResponse(req);
        if ("application/json".equals(resp.getContentType())) {
            String jsonText = resp.getText();
            scorePanluService.saveScorePanlu(jsonText);
        }
        System.out.println("页面加载调用完成 : " + resp.getURL());
		return "result";
	}
	
	
}
